﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JYStockAnalyser.Model;
using JYStockAnalyser.Face;
using JYStockAnalyser.ClientLib;

namespace JYStockAnalyser.WinUI
{
    public partial class FrmEstimate :  FrmFunctionBase
    {
        public FrmEstimate()
        {   
            InitializeComponent();        
        }

        private void FrmEstimate_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode) { this.stockinputer.BindData(); }

            YearQuarter[] minAndMaxYears = ClientCache.Instance.MinAndMaxQuarter;
            for (int i = minAndMaxYears[0].Year; i <= minAndMaxYears[1].Year; i++)
            {
                for (int quarter = 1; quarter <= 4; quarter++)
                {
                    if (i.Equals(minAndMaxYears[1].Year) && quarter > minAndMaxYears[1].Quarter) { break; }

                    this.cbEstimatePeriod.Items.Add(new YearQuarter(i, quarter));
                }
            }
        }

        private void btnReEstimate_Click(object sender, EventArgs e)
        {
            if (this.stockinputer.SelectedStockNO == "")
            {
                Tool.ShowInformationBox("请输入股票代码!");
                this.stockinputer.Focus();
                return;
            }

            if (this.cbEstimatePeriod.SelectedIndex == -1)
            {
                Tool.ShowInformationBox("请选择一个估值期");
                this.cbEstimatePeriod.Focus();
                return;
            }

            StockManageFace face = new StockManageFace();
            m_setEstimateResult(Tool.GetEstimateValueStrings(face.GetEstimateValue(stockinputer.SelectedStockNO, (YearQuarter)this.cbEstimatePeriod.SelectedItem)));
        }

        private void m_setEstimateResult(string[] estimateValues)
        {
            this.lblEstimateA.Text = estimateValues[0];
            this.lblEstimateB.Text = estimateValues[1];
            this.lblEstimateC.Text = estimateValues[2];
            this.lblEstimateD.Text = estimateValues[3];
            this.lblEstimateE.Text = estimateValues[4];
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void btnCompute_Click(object sender, EventArgs e)
        {
            string strPeriod = this.txtPeriod.Text.Trim();
            string strBeginValue = this.txtBeginValue.Text.Trim();
            string strEndValue = this.txtEndValue.Text.Trim();
            string strCompoundGrowthRate = this.txtCompoundGrowthRate.Text.Trim();

            //if (string.IsNullOrEmpty(strPeriod))
            //{ Tool.ShowInformationBox("复利计算期必须填写"); this.txtPeriod.Focus(); return; }

            int nullCount = 0;    
            if (string.IsNullOrEmpty(strBeginValue))
                nullCount += 1;
            if (string.IsNullOrEmpty(strPeriod))
                nullCount += 1;
            if (string.IsNullOrEmpty(strEndValue))
                nullCount += 1;
            if (string.IsNullOrEmpty(strCompoundGrowthRate))
                nullCount += 1;

            if (nullCount != 1)
            {
                Tool.ShowInformationBox("请输入三项数据，空出的一项系统自动计算");
            }

            int period;
            decimal beginValue;
            decimal endValue;
            decimal cgr;
        
            
            if (string.IsNullOrEmpty(strCompoundGrowthRate))
            {
                try {
                    period = int.Parse(strPeriod);
                    beginValue = decimal.Parse(strBeginValue);
                    endValue = decimal.Parse(strEndValue);
                }
                catch(System.FormatException) { 
                    Tool.ShowInformationBox("请正确输入数字");
                    return;
                }

                this.txtCompoundGrowthRate.Text = FinancialCalculator.GetCompoundGrowthRateString(beginValue, endValue, period).Replace("%", "");
                return;
            }

            if (string.IsNullOrEmpty(strBeginValue))
            {
                try
                {
                    period = int.Parse(strPeriod);
                    cgr = decimal.Parse(strCompoundGrowthRate);
                    endValue = decimal.Parse(strEndValue);
                }
                catch (System.FormatException)
                {
                    Tool.ShowInformationBox("请正确输入数字");
                    return;
                }

                this.txtBeginValue.Text = Tool.GetTheValueString(FinancialCalculator.CalculateBeginValueByCGR(endValue, cgr / 100, period));
                return;
            }

            if (string.IsNullOrEmpty(strEndValue))
            {
                try
                {
                    period = int.Parse(strPeriod);
                    cgr = decimal.Parse(strCompoundGrowthRate);
                    beginValue = decimal.Parse(strBeginValue);
                }
                catch (System.FormatException)
                {
                    Tool.ShowInformationBox("请正确输入数字");
                    return;
                }

                this.txtEndValue.Text = Tool.GetTheValueString(FinancialCalculator.CalculateEndValueByCGR(beginValue, cgr / 100, period));
                return;
            }

            if (string.IsNullOrEmpty(strPeriod))
            {
                try
                {
                    cgr = decimal.Parse(strCompoundGrowthRate);
                    beginValue = decimal.Parse(strBeginValue);
                    endValue = decimal.Parse(strEndValue);
                }
                catch (System.FormatException)
                {
                    Tool.ShowInformationBox("请正确输入数字");
                    return;
                }

                this.txtPeriod.Text = Tool.GetTheValueString(FinancialCalculator.CalculatePeriodByCGR(beginValue, cgr / 100, endValue));
                return;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtPeriod.Text = "";
            this.txtBeginValue.Text = "";
            this.txtEndValue.Text = "";
            this.txtCompoundGrowthRate.Text = "";
        }
    }
}
